// pages/material/categoryMaterial/categoryMaterial.js
let api = require("../../../api/materialApi")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    materialList:[],
    next: null,
    category_id: -1,
    categoryName: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log(options)
    that.setData({ 
      category_id: options.category_id,
      categoryName: options.category_name
    })
    api.getCategoryMaterial(that.data.category_id, null).then(res=>{
      console.log(res)
      that.setData({
         materialList: res.data.results,
         next: res.data.next
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    if(that.data.next == null){

      wx.showToast({
        title:"没有更多了",
        icon: 'none',
        duration: 2000
      })

    }else{
      api.getCategoryMaterial(that.data.category_id ,that.data.next).then(res=>{
        let results = res.data.results
        let _materialList = that.data.materialList
        for(let i = 0; i < results.length; i++){
          _materialList.push(results[i])
        }
        that.setData({
          materialList: _materialList,
          next: res.data.next
        })
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})